package com.vietthangif.project.Views;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.vietthangif.project.contact.Contact;
import com.vietthangif.project.contact.Entry;
import com.vietthangif.project.silentcontact.R;

public class ContactActivity extends Activity{
	public static final String PREFS_NAME = "MyApp";
	protected static final int SUB_ACTIVITY_REQUEST_CODE = 100;
	private static final String TAG = ContactActivity.class.getName();
	
	protected BroadcastReceiver broadcastReceiver; 
	private static ListView contactList;
	private Context context;
	private Resources resources;
	private ContactAdapter contactAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		context = getApplicationContext();
		resources = getResources();
		
		registerProgressReceiver();
		
		contactList = (ListView)findViewById(R.id.mylist);
		
		SharedPreferences setting = getSharedPreferences(PREFS_NAME, 0);
		if(!setting.getString("isFirstLaunch", "").toString().equals("second")) {
			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("isFirstLaunch", "second");
			editor.commit();
			
			Intent intent = new Intent(this, SelectImportActivity.class);
			startActivityForResult(intent, SUB_ACTIVITY_REQUEST_CODE);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_contact, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_loadLocal:
	        	ArrayList<Entry> contacts = Contact.loadContact(getApplicationContext());
	        	if(contacts!=null) {
	        		loadList(contacts);
	        	}
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	protected void loadList(ArrayList<Entry> contacts) {
//		ArrayAdapter<String> contactAdapter = new ArrayAdapter<String>(this, R.layout.contact_row, contacts);
		contactAdapter = new ContactAdapter(ContactActivity.this, contacts);
		contactList.setAdapter(contactAdapter); 
	}

	@Override
	 public void onActivityResult(int requestCode,int resultCode,Intent data)
	 {
//		super.onActivityResult(requestCode, resultCode, data);
//		ArrayList<String> contacts = data.getStringArrayListExtra("contacts");
//		if(contacts != null && contacts.size()!=0) {
//			loadList(contacts);
//		}
     }
	
	private void registerProgressReceiver() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Contact.LOAD_CONTACT_ERROR);
		intentFilter.addAction(Contact.NUM_CONTACT_ZERO);
		intentFilter.addAction(Contact.PHONE_CONTACT_ERROR);
		
		broadcastReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if(action.equals(Contact.LOAD_CONTACT_ERROR)) {
					Toast.makeText(context, resources.getString(R.string.toast_errorNullCursor), Toast.LENGTH_LONG).show();
				} else 
				if(action.equals(Contact.NUM_CONTACT_ZERO)) {
					Toast.makeText(context, resources.getString(R.string.toast_errorZeroCursor), Toast.LENGTH_LONG).show();
				}
				if(action.equals(Contact.NUM_CONTACT_ZERO)) {
					Toast.makeText(context, resources.getString(R.string.toast_errorLoadPhone), Toast.LENGTH_LONG).show();
				}
			}
		};
		
		registerReceiver(broadcastReceiver, intentFilter);        
	}
}
